package com.example.javamaday.leetcode;

// Z字形变换
public class LeetCodeTest0006 {

    public static void main(String[] args) {
        System.out.println(convert("ABAASDASD", 2));
    }

    public static String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        char[] charArray = s.toCharArray();
        char[][] content = new char[charArray.length][numRows];
        int xIndex = 0;
        int yIndex = 0;
        boolean isRevert = false;

        for (int i = 0; i < charArray.length; i++) {
            content[xIndex][yIndex] = charArray[i];
            if (yIndex == numRows - 1 && !isRevert) {
                isRevert = true;
            }
            if (yIndex == 0 && isRevert) {
                isRevert = false;
            }
            if (isRevert) {
                yIndex--;
                xIndex++;
            } else {
                yIndex++;
            }
        }
        StringBuilder builder = new StringBuilder();
        for (int j = 0; j < numRows; j++) {
            for (int i = 0; i <= xIndex; i++) {
                if (content[i][j] > 0) {
                    builder.append(content[i][j]);
                }
            }
        }
        return builder.toString();
    }
}
